<?php
class BaseAction extends Action{
	
	public function _initialize() {
		//清除当前session以免已直接通过小心意登录影响到插件
		$_session_user_id	=	intval($_SESSION['mid']);
		$p_uid = $_REQUEST['p_uid'];
		$p_key = $_REQUEST['p_key'];
		//echo $_session_user_id."<br>";
		//echo $p_uid."-".$p_key."<br>";
		//dump($_SESSION);
		//如果当前用户不与session中的uid一致则取消重新清空session
		if(!$_session_user_id||$p_uid!=$_session_user_id){
			service('Passport')->logoutLocal(false);
			$user = model('Agent')->getAgent($p_uid);
			//echo "user=";dump($user);
			//若：uid与key不对应，或是用户状态不正确，或无agent数据则提示错误。
			if(empty($user)||!$user||$user['role']!='agent'){
				$this->error('插件认证提示：请加盟小心意！获取正确的插件代码！');
			}
			if($user['enable']!='Y'||!$user['is_active']){
				$this->error('插件认证提示：插件用户状态异常！');
			}
			if(empty($user['agent'])||$user['agent']['enable']!='Y'){
				$this->error('插件认证提示：该站点目前不能使用小心意插件！');
			}
			if($p_key!=$user['agent']['key']){
				$this->error('插件认证提示：密钥错误！');
			}
			$_SESSION['mid'] = $p_uid;
			//$_SESSION['mid'] = $p_uid;
			$_SESSION['userInfo'] = $user;
		}
		$this->mid			=	intval($_SESSION['mid']);//登录用户ID
		$this->uid	=	$this->mid;

		$this->user	=	$_SESSION['userInfo'];
		//dump($this->user);
		global $ts;
		$ts['user']	=	$this->user;
		
		$this->assign('mid',$this->mid);
		$this->assign('uid',$this->uid);
		$this->assign('user',$this->user);
	}
	
	protected function _before_action(){
		echo "do _before_action<br>";
	} 
	
	protected function _after_action(){
		echo "do _after_action<br>";
	} 
}
